package Model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Vector;

import Messaging.Notification;
import Messaging.NotificationManager;
import Ui.MessagePopUp;

/*
 * @author Paolo Cordova
 */

public class CommCardSpace extends Space implements Serializable 
{
	ArrayList<Player> Players_;
	Vector<Card> Stack_;
	ListIterator<Card> Itr_;
	private static CommCardSpace INSTANCE = null;
	public static final int NUMBER = 12;
	
	final static long serialVersionUID = 207;

	protected CommCardSpace(ArrayList<Player> players) 
	{
		Stack_ = new Vector<Card>(NUMBER);
		
		Stack_.add(new CardTypeOutOfJail());
		Stack_.add(new CardTypeMove("Advance to B.&0. Railroad", 25));
		Stack_.add(new CardTypePayFund("Pay hospital fees amounting to $100", 100));
		Stack_.add(new CardTypeWinMoney("Bank error in your favor. Collect $75!", 75));
		Stack_.add(new CardTypePayFund("Doctor's fee. Pay $50!", 50));
		Stack_.add(new CardTypeWinMoney("Income Tax refund. Collect $20!", 20));
		Stack_.add(new CardTypeWinMoney("Second Prize in a beauty contest! Collect $20", 20));
		Stack_.add(new CardTypeMove("Advance to Go", 0)); 
		Stack_.add(new CardTypeMove("Go To Jail!.", 10));
		Stack_.add(new CardTypeWinMoney("Life insurance matures. Collect $100", 100));
		Stack_.add(new CardTypeWinMoney("You have won second prize in a beauty contest. Collect $10", 10));
		Stack_.add(new CardTypePayFund("Had a lyposuction. Pay $100", 100));
		
		Players_ = players;
		Itr_ = Stack_.listIterator();
	}
	
	public static CommCardSpace Instance(ArrayList<Player> players) 
	{
		if (INSTANCE == null) 
		{
			INSTANCE = new CommCardSpace(players);
		}
		return INSTANCE;
	}
	
	public String toString() 
	{
		return "Community Chest";
	}
	
	public void landOn(Player p) 
	{
		Card card = drawCard();
		NotificationManager.getInstance().notifyObservers(Notification.SHOW_CARD, card);
		NotificationManager.getInstance().notifyObservers(Notification.MESSAGE_POPUP,  p.getName() + " drew the following Community Chest card: " + card.getMessage(), false);
		new MessagePopUp(card, p, Players_, "Community Chest");
	}
	
	public Card drawCard() 
	{
		
		if (Itr_.hasNext()) 
		{
			return Itr_.next();
		}
		
		else 
		{
			while(Itr_.hasPrevious()) 
			{
				Itr_.previous();
			}
			return Itr_.next();
		}
	}
	
	public void ShuffleCards() 
	{
		Collections.shuffle(Stack_);
	}
}
